/**
 * 2013-6-24 下午10:29:11 Created By niexiaoqiang
 */

package com.xiaoqiang.MtwV2ForMoblie;

import java.io.File;
import java.io.IOException;

import android.os.Looper;

import com.xiaoqiang.MtwV2ForMoblie.listener.MainActivityConfigFileDownLoadSizeChangedListener;
import com.xiaoqiang.util.Logger;
import com.xiaoqiang.util.MtwProperties;
import com.xiaoqiang.util.download.HttpDownload;

/**
 * 下载配置文件的线程,在AppStartAction中调用
 */
public class MtwDownloadConfigFileThread extends Thread {
	private String filehttpurl;
	private String downloadfilename;
	private static Logger log = new Logger(MtwDownloadConfigFileThread.class);
	MainActivity mainActivity;
	private MtwProperties mtwProperties;

	public MtwDownloadConfigFileThread(MainActivity mainActivity) {
		this.mainActivity = mainActivity;
		try {
			mtwProperties = MtwProperties.newInstance();
		}
		catch (IOException e) {
			log.error(e);
		}
		this.filehttpurl = mtwProperties.getProperties("configfilelocation");
		this.downloadfilename = filehttpurl.substring(filehttpurl.lastIndexOf(File.separator) + 1);
	}

	@Override
	public void run() {
		Looper.prepare();
		HttpDownload httpDownload = new HttpDownload(downloadfilename);
		MainActivityConfigFileDownLoadSizeChangedListener mainActivitydownlistener = new MainActivityConfigFileDownLoadSizeChangedListener(mainActivity);
		httpDownload.addServerMsgListener(mainActivitydownlistener);
		try {
			log.debug("start donwlaodfile" + filehttpurl);
			httpDownload.DownFile(filehttpurl);
		}
		catch (Exception e) {
			log.equals(e);
		}
	}
}
